package com.happyelements.components
{
	import com.bit101.components.PushButton;
	import com.happyelements.data.DataModel;

	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;

	public class AssetsList extends Sprite
	{

		private var _listDictionary:Dictionary;
		
		private var _list:List;

		private var PADDING:Number;
		
		private var _listItemClickHandler:Function;
		
		private var _generateHandler:Function;
		
		public function AssetsList(listItemClickHandler:Function, generateHandler:Function, width:Number=100, height:Number=300)
		{
			_listItemClickHandler = listItemClickHandler;
			
			_generateHandler = generateHandler;
			
			initilizeAssets(width, height);
		}
		
		private function initilizeAssets(width:Number, height:Number):void
		{
			_listDictionary = new Dictionary(true);
			_list = new List(width, height);
			
			addChild(_list);
			
			var generateConfigButton:PushButton = new PushButton(this, 0, 0, "Generate Map Config", generateMapConfig);
			PADDING = 10;
			generateConfigButton.y = _list.height + PADDING;
			generateConfigButton.x = (_list.width - generateConfigButton.width) / 2;
			
			readXML();
		}
		
		private function readXML():void
		{
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest(GlobalPara.ASSETS_BUILDING_CONFIG);
			
			loader.addEventListener(Event.COMPLETE, onXMLLoaded);
			loader.load(request);
		}
		
		private function onXMLLoaded(event:Event):void
		{
			var xml:XML = XML(event.target.data);
			
			for (var item:Object in xml..building)
			{
				var dataModel:DataModel = new DataModel();
				dataModel.id = xml..building[item].@id;
				dataModel.source = xml..building[item].@source;
				dataModel.ref = xml..building[item].@ref;
				dataModel.cols = xml..building[item].@cols;
				dataModel.rows = xml..building[item].@rows;
				
				generateList(dataModel);
			}
		}
		
		private function generateList(model:DataModel):void
		{
			var loader:Loader = new Loader();
			var request:URLRequest = new URLRequest(GlobalPara.ASSETS_BASE_PATH + model.source);
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void
			{
				var domain:ApplicationDomain = (event.target as LoaderInfo).applicationDomain;
				var ANIMATION_CLASS:Class = domain.getDefinition(model.ref) as Class;
				var mc:MovieClip = new ANIMATION_CLASS() as MovieClip;
				
				_listDictionary[mc] = model;
				
				_list.addItem(mc);
				
				mc.addEventListener(MouseEvent.CLICK, onListMCClick);
			});
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void
			{
				trace("IOError " + (event.target as LoaderInfo).url);
			});
			
			loader.load(request);
		}
		
		private function generateMapConfig(event:Event):void
		{
			_generateHandler();
		}
		
		private function onListMCClick(event:Event):void
		{
			_listItemClickHandler(_listDictionary[event.currentTarget]);
		}
	}
}